package org.bravof.differ.controller;

import lombok.extern.log4j.Log4j;
import org.bravof.differ.dao.DetailDAO;
import org.bravof.differ.entity.DetailPO;
import org.bravof.differ.query.PageModel;
import org.bravof.differ.query.Query;
import org.bravof.differ.query.ResultModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据库配置详情Controller
 */
@Log4j
@RestController
@RequestMapping("/detail")
public class DetailController {
    @Resource
    private DetailDAO detailDAO;

    @RequestMapping("/list")
    public PageModel<DetailPO> list(Query query, PageModel<DetailPO> pageModel) {
        List<DetailPO> list = detailDAO.findDetailListByQuery(query, 0, 100);
        pageModel.setModel(list);
        pageModel.setTotal(0);
        return pageModel;
    }

    @RequestMapping("/editDetail")
    public ResultModel<DetailPO> editDetail(DetailPO detailPO) {
        Integer id = detailPO.getId();
        if (null == id) {
            log.info("新增配置");
            detailDAO.addDetail(detailPO);
        } else {
            log.info("更新配置");
            detailDAO.updateDetail(detailPO);
        }
        return new ResultModel<>();
    }

    @RequestMapping("/delete")
    public ResultModel delete(Integer id) {
        detailDAO.deleteDetail(id);
        return new ResultModel<>();
    }
}
